home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
BUS
/
TMCM Software and Labs.sit
/
Software for TMCM 7_95
/
Files For Lab 8
/
EchoNums
< prev
next >
Wrap
Text File
|
1995-07-13
|
5KB
|
100 lines
; This program illustrates the I/O capabilities of xComputer.
; To use this program, you must turn on the "Use I/O Services"
; option in the "Options" menu. (If this option is off, the
; computer will not recognize the I/O instructions gtc and ptc
; when the program is run.)
; The program reads integers input by the user. An integer
; can consist of several digits, such as "123". The user types
; these digits and the program keeps track of the number typed.
; The user indicates the end of a number by typeing a ".".
; When the computer reads the ".", it echos the number to output.
; This continues until the user presses return. At that point,
; the program ends. Aside from digits ("0" through "9"), periods,
; and carriage returns, all other characters are ignored.
; (Note that if the user types in too many digits to be represented
; by the xComputer, the output number will be incorrect. Numbers
; on the xComputer are limited to 16 bits.)
lod-c 0 ; "Num" holds the number being input.
sto num ; Start with Num = 0, before anything is typed.
get_ch: gtc ; Main loop: Get the next char from input.
jmz get_ch
sto ch ; Save the non-zero char in "ch" for processing.
; Note that it is actually the ASCII code of
; ch that is stored.
lod ch ; Compare the input char to "carriage return", and
sub-c 13 ; jump to "done" if ch is a carriage return.
jmz done
lod ch ; Compare the input char to ".". If it is ".",
sub-c '. ; then jump to "end_num", where the num will
jmz end_num ; be output.
; The next six instructions check whether the
; input character is in the range from "0" to
; "9". If not, the program ignores the char
; and jumps back to get_ch to get the next input
; character.
lod-c '9 ; Check if ch if bigger than "9" by subtracting
sub ch ; ch from the ASCII code of "9". If the result
jmn get_ch ; is negative, then ch is > "9", so ignore it
; and go back to get the next char.
lod ch ; Check if ch is less than "0" by subtracting
sub-c '0 ; the ASCII code of "0" from ch. If the result
jmn get_ch ; is negative, then ch < "0", so ignore it
; and go back to get the next char.
sto digit ; After subtracting the ASCII code of "0" from
; ch, the number in the accumulator happens
; to be the actual number represented by the
; character ch. (If ch is "0", the number is
; 0; if ch is "1", the number is 1; etc. This
; single-digit number is stored in memory
; location "digit".
; The next 8 instructions adjust the value stored
; in location "num" to reflect the new digit just
; typed in by the user. The object is to compute
; 10 * num + digit, and to store the result back
; into num. (For example, if num is "12" and the
; user types in a 3, then num becomes 10 * 12 + 3,
; or 123.) The programming here is a bit tricky.
lod num ; Get the original value of num. (Call it N.)
shl ; Shifting left multiplies N by 2.
sto num ; Sets num = 2*N.
shl ; Shifting left again sets the accumulator = 4*N.
shl ; Shifting left again sets the accumulator = 8*N.
add num ; Adds 2*N to 8*N, giving 10*N in the accumulator.
add digit ; Adds digit to 10*N.
sto num ; Stores (18*N + digit) back into num.
jmp get_ch ; Go back to process the nest input char.
end_num: lod num ; The program jumps here when the user types ".".
pti ; Get the value of num and write it to output
; as an integer.
lod-c 0 ; Start over with num = 0.
sto num
jmp get_ch ; Go back to read and process the next character.
done: hlt ; The program jumps here when the user types
; a carriage return. The program ends.
ch: data ; Location to hold character input by user.
digit: data ; Location to hold the single-digit number
; represented by an input character in the
; range "0" through "9".
num: data ; Location to hold the (multi-digit) number
; begin typed in by the user.